package cn.bocweb.spinning.view.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.MatchInviteBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.util.WXUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.dialog.ShareDialog;
import de.greenrobot.event.EventBus;

public class FirstRewardActivity extends BaseActivity implements ShareDialog.OnShareListener {

    String url;
    ShareDialog mShareDialog;
    UserPresenter mPresenter;
    int friend_1_timeline_2;

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.first_reward)
    WebView mFirstReward;
    @BindColor(R.color.white)
    int white;
    @Bind(R.id.first_reward_share)
    ImageView mFirstRewardShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_reward);
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);
        mPresenter = new UserPresenterImp();
        mShareDialog = new ShareDialog(this);
        mShareDialog.setOnShareListener(this);

        dialog.show();

        initToolbar();
        initIntent();
        initWebView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void onEventMainThread(MatchInviteBean bean) {
        if (bean.getReturn_code() == 0) {
            if (friend_1_timeline_2 == 1) {
                WXUtil.WXShareToFriend(bean.getUrl(), this);
            } else {
                WXUtil.WXShareToTimeline(bean.getUrl(), this);
            }
        }
    }

    private void initWebView() {
        mFirstReward.loadUrl(url);
        mFirstReward.getSettings().setJavaScriptEnabled(true);
        mFirstReward.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("http://code/")) {
                    ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                    clip.setText(url.substring(12, url.length()));
                    DeviceUtil.Log("code view = " + url);
                    DeviceUtil.Log("code = " + url.substring(12, url.length()));
                    return true;
                }
                return false;
            }
        });
    }

    private void initIntent() {
        Intent intent = getIntent();
        url = intent.getStringExtra(Constant.TO_FIRST_REWARD);
    }

    private void initToolbar() {
        mToolbar.setTitle("知识大奖");
        mToolbar.setTitleTextColor(white);
        mToolbar.setNavigationIcon(R.mipmap.btn_exit);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.first_reward_share)
    public void onClick() {
        mShareDialog.show();
    }

    @Override
    public void shareTimeLine() {
        mPresenter.matchInvite();
        friend_1_timeline_2 = 2;
    }

    @Override
    public void shareFriend() {
        mPresenter.matchInvite();
        friend_1_timeline_2 = 1;
    }
}
